/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.garcomeletronico.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexao {
    
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String URL = "jdbc:sqlserver://localhost:1433;";
    private static final String user = "sa";
    private static final String pass = "123456";
    
    private Connection connection = null;
    
    static {
        try {
            Class.forName(DRIVER);
            System.out.println("Conectado ao Banco de Dados");      
        } catch (Exception e) {
            try {
                throw new Exception(e.getMessage());
            } catch (Exception ex) {
                Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

    public Connection getConexao() throws SQLException {
        if (connection == null) {
            connection = DriverManager.getConnection(URL + "databaseName= bdge;", user, pass);
        }
        return connection;
    }
    
    
    public void fecharConexao() throws SQLException {
        if (connection != null) {
            connection.close();
            System.out.println("Conexão fechada com sucesso!");
        }
    }

}